package com.pig4cloud.pig.flow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.flow.api.dto.ApprovalDTO;
import com.pig4cloud.pig.flow.api.dto.FlowSearchDTO;
import com.pig4cloud.pig.flow.api.dto.StartFlowDTO;
import com.pig4cloud.pig.flow.api.dto.SuspensionDTO;
import com.pig4cloud.pig.flow.pojo.dto.FlowDTO;
import com.pig4cloud.pig.flow.service.FlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * @author mobai
 */
@RestController
@RequiredArgsConstructor
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@RequestMapping("/flow")
public class FlowController {
	private final FlowService flowService;

	@PostMapping("/deployment")
	public R test(@RequestBody FlowDTO flowDTO) {
		return flowService.deployment(flowDTO);
	}

	/**
	 * 获取流程图
	 */
	@GetMapping("/getFlowJson/{flowKey}")
	public R getFlowJson(@PathVariable String flowKey, @RequestParam(required = false) Integer version) {
		return flowService.getFlowJson(flowKey, version);
	}

	/**
	 * 根据process_instance_id 获取流程图
	 */
	@GetMapping("/getFlowJsonByProcessInstanceId/{processInstanceId}")
	public R getFlowJsonByProcessInstanceId(@PathVariable String processInstanceId) {
		return flowService.getFlowJsonByProcessInstanceId(processInstanceId);
	}


	/**
	 * 获取流程所有版本号
	 */

	@GetMapping("/getFlowVersion/{key}")
	public R getFlowVersion(@PathVariable String key) {
		return flowService.getFlowVersion(key);
	}

	/**
	 * 修改流程版本号
	 */
	@PutMapping("/updateFlowVersion/{key}/{version}")
	public R updateFlowVersion(@PathVariable String key, @PathVariable Integer version) {
		return flowService.updateFlowVersion(key, version);
	}


	/**
	 * 发起流程
	 */
	@PostMapping("/startProcessInstance")
	public R<String> startProcessInstance(@RequestBody StartFlowDTO startFlowDTO) {
		return flowService.startProcessInstance(startFlowDTO);
	}

	/**
	 * 获取流程实例审批历史节点
	 */

	@GetMapping("/getHistoryNode/{processInstanceId}")
	public R getHistoryNode(@PathVariable String processInstanceId) {
		return flowService.getHistoryNode(processInstanceId);
	}

	/**
	 * 审批节点
	 */
	@PostMapping("/completeTask")
	public R completeTask(@RequestBody ApprovalDTO approvalDTO) {
		return flowService.completeTask(approvalDTO);
	}

	/**
	 * 挂起流程
	 */

	@PostMapping("/updateSuspendState")
	public R updateSuspendState(@RequestBody SuspensionDTO suspensionDTO) {
		return flowService.updateSuspendState(suspensionDTO);
	}

	/**
	 * 获取我发起的流程分页查询
	 *
	 * @param page          分页对象
	 * @param flowSearchDTO 实体表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/myStartPage")
	public R getMetaEntityPage(@ParameterObject Page page, @ParameterObject FlowSearchDTO flowSearchDTO) {
		return R.ok(flowService.myStartPage(page, flowSearchDTO));
	}

	/**
	 * 由我审批的流程分页查询
	 *
	 * @param page 分页对象
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/myApprovalPage")
	public R myApprovalPage(@ParameterObject Page page, FlowSearchDTO flowSearchDTO) {
		return R.ok(flowService.myApprovalPage(page, flowSearchDTO));
	}


	/**
	 * 我的已办分页查询
	 *
	 * @param page 分页对象
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/myDonePage")
	public R myDonePage(@ParameterObject Page page, FlowSearchDTO flowSearchDTO) {
		return R.ok(flowService.myDonePage(page, flowSearchDTO));
	}


	/**
	 * 获取任务权限
	 */

	@GetMapping("/getTaskPermission/{taskId}")
	public R getTaskPermission(@PathVariable String taskId) {
		return flowService.getTaskPermission(taskId);
	}


	/**
	 * 终止流程
	 */
	@PostMapping("/terminationProcess")
	public R terminationProcess(@RequestBody ApprovalDTO approvalDTO) {
		return flowService.terminationProcess(approvalDTO);
	}

}
